package com.wjw.java.io.buffered_;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

/**
 * 使用BufferedReader读取文件
 */
public class BufferedReader_ {
    public static void main(String[] args) {
        readData();
    }

    //1.读取txt中的数据
    public static void readData(){
        //文件路径
        String filePath = "f:\\IO\\test_buffered_reader.txt";
        BufferedReader br = null;
        String line = "";
        try {
            br = new BufferedReader(new FileReader(filePath));
            //循环读取txt中的数据,并在控制台打印
            while ((line = br.readLine()) != null){
                System.out.println(line);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //这里只需关闭BufferedReader,它的底层会去关闭传入的FileReader
            if(br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
